-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Audio visualization helpers #474
base: main
Are you sure you want to change the base?
Conversation
vDSP_meanv(ptr, 1, &mean, UInt(count)) | ||
} | ||
return mean | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simplify this function using ArraySlice to give responsibility for safe pointer arithmetic back to swift instead:
@inline(__always) private func _computeAverage(_ array: ArraySlice<Float>) -> Float {
var mean: Float = 0
array.withUnsafeBufferPointer { bufferPtr in
vDSP_meanv(bufferPtr.baseAddress!, 1, &mean, vDSP_Length(array.count))
}
return mean
}
then you call it above with _computeAverage(magnitudes[magsStartIdx..<magsEndIdx])
let count = stopIdx - startIdx | ||
array.withUnsafeBufferPointer { bufferPtr in | ||
let ptr = bufferPtr.baseAddress! + startIdx | ||
vDSP_meanv(ptr, 1, &mean, UInt(count)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vDSP_Length(count)
public let scaleType: ScaleType | ||
|
||
private let bufferHalfSize: Int | ||
private let bufferLog2Size: Int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should consider declaring these int types as vDSP_Length
instead. At the very least, they should be UInt
here and cast to vDSP_Length
when passed to accelerate later
deinit { | ||
vDSP_destroy_fftsetup(fftSetup) | ||
realPointer.deallocate() | ||
imaginaryPointer.deallocate() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason you're managing raw pointers in this class rather than just using arrays and nested withUnsafeMutableBufferPointer calls as in the apple sample code? looks scary to me 🤷 https://developer.apple.com/documentation/accelerate/vdsp/fast_fourier_transforms/finding_the_component_frequencies_in_a_composite_sine_wave
import Foundation | ||
|
||
// Simple ring-buffer used for internal audio processing. Not thread-safe. | ||
class FloatRingBuffer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't really matter but you could call this RingBuffer<T: Numeric>
and replace all of the Float
with T
below and it would still work as-is.
No description provided.